package com.factory.test.activity;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.yx.YxDeviceManager;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.SeekBar;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivitySystemOperateBinding;
import com.yecontech.sdk.base.vo.Constants;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
 * 获取系统信息
 */
public class SystemOperateActivity extends BaseTestActivity {

    private ActivitySystemOperateBinding binding;
YxDeviceManager yx;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_system_operate);
        binding.setViewClick(new ViewClick());
yx=YxDeviceManager.getInstance(this);

        Constants.USBMode usbMode = sdkService.getUSBMode();
        if(usbMode == Constants.USBMode.USB_DEVICE){
            binding.usbDevice.setChecked(true);
        }else if(usbMode == Constants.USBMode.USB_HOST){
            binding.usbHost.setChecked(true);
        }

        binding.usbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == binding.usbDevice.getId()){
                    sdkService.setUSBMode(Constants.USBMode.USB_DEVICE);

                }else if(checkedId == binding.usbHost.getId()){
                    sdkService.setUSBMode(Constants.USBMode.USB_HOST);

                }
            }
        });

        binding.navigationBarShow.setChecked(true);
//        sdkService.navigationBarShow();

        binding.navigationBarGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == binding.navigationBarShow.getId()){
                    yx.setNavBar(true);

                }else if(checkedId == binding.navigationBarHide.getId()){
                    yx.setNavBar(false);


                }
            }
        });


        binding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(progress <= 0){
                    progress = 1;
                }
                sdkService.setBrightness(progress);
                binding.message.setText(String.format("屏幕亮度: %d%%", progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        binding.seekBar.setProgress(sdkService.getBrightness());

        IntentFilter netFilter = new IntentFilter();
        netFilter.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(networkReceiver, netFilter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initDateTime();
    }

    private BroadcastReceiver networkReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(Intent.ACTION_TIME_TICK.equals(intent.getAction())){
                Log.i("Receiver", "时间发生变化");
                initDateTime();
            }
        }
    };

    private void initDateTime() {
        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        binding.dateTime.setText(dateFormat.format(currentDate));
    }


    private int clickCount = 0;
    public class ViewClick{

        public void onShutdown(){
            sdkService.shutdown(false);
        }
        public void onReboot(){
            sdkService.reboot();
        }

        public void onPlusTime(){
            SystemClock.setCurrentTimeMillis(System.currentTimeMillis() + (1000 * 60));
        }

        public void onReduceTime(){
            SystemClock.setCurrentTimeMillis(System.currentTimeMillis() - (1000 * 60));
        }

        public synchronized void onDataTime(){
            clickCount++;
            if(clickCount >= 10){
                clickCount = 0;
                binding.resetSystem.setVisibility(View.VISIBLE);
            }
        }

        public void onResetSystem(){
            sdkService.restoreFactorySettings();
//            boolean mEraseSdCard = false;
//            Intent intent = new Intent("android.intent.action.MASTER_CLEAR");
//            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
//            intent.setPackage("android");
//            intent.putExtra("reason", "MasterClearConfirm");
//            intent.putExtra("android.intent.extra.WIPE_EXTERNAL_STORAGE", mEraseSdCard);
//            intent.putExtra("android.intent.extra.EXTRA_WIPE_ESIMS", mEraseSdCard);
//            context.sendBroadcast(intent);
        }
    }
}
